package nid.gallery 
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import nid.gallery.ThumbHolder;
	import nid.gallery.view.AudioView;
	import nid.gallery.view.ImageView;
	import nid.gallery.view.SWFView;
	import nid.gallery.view.ui.NextButton;
	import nid.gallery.view.ui.PrevButton;
	import nid.gallery.view.UnknownView;
	import nid.gallery.view.VideoView;
	/**
	 * ...
	 * @author Nidin P Vinayakan
	 */
	public class View extends Sprite
	{
		[Embed(source='view/ui/images/next.png')]
		private var ImageSrc:Class;
		
		public var nextBtn:NextButton
		public var prevBtn:PrevButton
		
		private var thumbHolder:ThumbHolder;
		private var currentMedia:String;
		private var currentView:IView;
		
		/**
		 * Views
		 */
		public var imageView	:ImageView;
		public var audioView	:AudioView;
		public var videoView	:VideoView;
		public var swfView		:SWFView;
		public var unknownView	:UnknownView;
		
		public var xml:XML;
		
		public function View() 
		{
			addEventListener(Event.ADDED_TO_STAGE, configStage);
			
		}
		
		private function configStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, configStage);
			
			stage.align 	= StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			stage.addEventListener(Event.RESIZE, align);
			
			imageView 	= new ImageView();
			videoView 	= new VideoView();
			audioView 	= new AudioView();
			swfView 	= new SWFView();
			unknownView = new UnknownView();
			
			nextBtn = new NextButton(ImageSrc);
			prevBtn = new PrevButton(ImageSrc);
			addChild(nextBtn);
			prevBtn.x = 10;
			addChild(prevBtn);
			align();
		}
		
		private function align(e:Event=null):void 
		{
			prevBtn.y = (stage.stageHeight - 70) / 2 - nextBtn.height / 2;
			nextBtn.x = stage.stageWidth - nextBtn.width - 10;
			nextBtn.y = (stage.stageHeight - 70) / 2 - nextBtn.height / 2;
			if (thumbHolder != null)
			{
				thumbHolder.y = stage.stageHeight - thumbHolder.height - 5;
				thumbHolder.align();
			}
			if (currentView != null) currentView.align();
		}
		public function build(_xml:XML):void
		{
			/**
			 * Create XML object for access xml data all over the script 
			 */
			xml = _xml;
			/**
			 * Create a container sprite for hold all thumbnails 
			 */
			thumbHolder = new ThumbHolder();
			thumbHolder.x = 10;
			thumbHolder.y = stage.stageHeight - 70;
			addChild(thumbHolder);
			trace('length:'+xml.media.length());
			/**
			 * Create Thumbs and add to thumb container
			 */
			for (var i:int = 0; i < xml.media.length(); i++)
			{
				var thumb:Thumb = new Thumb(xml.media[i]); // pass xml data to thumbnail class
				
				thumb.id = i; // set thumb id
				
				thumb.x = i * 70; // set thumb x position
				
				thumbHolder.addThumb(thumb); // add thumb to container
				
				/**
				 * Add event listener change media when user click on thumbnails
				 */
				thumb.addEventListener(MouseEvent.CLICK, changeMedia);
				
			}
			
			/**
			 * Load first media
			 */
			push(xml.media[0]);
			
		}
		private function changeMedia(e:MouseEvent):void
		{
			/**
			 * Load Media
			 */
			push(xml.media[e.currentTarget.id]);
		}
		
		public function push(media:XML):void
		{
			
			cleanStage();
			
			if (String(media.@type) == "")
			{
				
			}
			else 
			{
				
				switch(String(media.@type))
				{
					case "image":
					{
						addChild(imageView);
						imageView.push(media);
						currentView = imageView;
					}
					break;
					
					case "audio":
					{
						addChild(audioView);
						audioView.push(media);
						currentView = audioView;
					}
					break;
					
					case "video":
					{
						addChild(videoView);
						videoView.push(media);
						currentView = videoView;
					}
					break;
					
					case "swf":
					{
						addChild(swfView);
						swfView.push(media);
						currentView = swfView;
					}
					break;
					
					default:
					{
						addChild(unknownView);
						unknownView.push(media);
						currentView = unknownView;
					}
					break;
				}
			}
		}
		private function cleanStage():void
		{
			if (currentView != null)
			{
				removeChild(DisplayObject(currentView));
				currentView.clear();
				currentView = null;
			}
		}
		private function onIOError(e:IOErrorEvent):void 
		{
			trace('io error');
			dispatchEvent(e);
		}
		
		private function onComplete(e:Event):void 
		{
			trace(e.target.content);
		}
	}

}